package com.fare.taluo.action.plane.income;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.Sheet;

import com.fare.taluo.io.excel.ExcelImportException;
import com.fare.taluo.io.excel.ImportExcelClassic;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.util.StringUtils;
import com.fare.taluo.vo.income.plfIncomeVO;

public class PlfIncomeExcelImport extends ImportExcelClassic{
	
	/**
	 * 从第几行开始读取数据
	 */
	private int startRow = 3;
	private Sheet sheet;
	@Override
	protected Object importData() throws ExcelImportException {
		
		Cell[] cells = null;
//		String id=null;
		 String companyCode =null;		
		 String lineCode =null;
		 String flightInfo =null;
		 String flightDay =null;
		 String price =null;
		 String occupRate =null;
		 String income =null;
		 Date flightDay2=null;
		 String flightDay3=null;
//		 List<TaskTemplateEntity> templates = new ArrayList<TaskTemplateEntity>();
		 
		 List<plfIncomeVO> incomes = new ArrayList<plfIncomeVO>();
		 plfIncomeVO vo = null;
		 StringBuilder sbuilder = new StringBuilder();
		 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");    
		 
		 try{
				for (int i = startRow; i < sheet.getRows()-1; i++) {
					cells = sheet.getRow(i);
//					id = cells[0].getContents().trim();
					companyCode = cells[1].getContents().trim();
					lineCode = cells[2].getContents().trim();
					flightInfo = cells[3].getContents().trim();					
					flightDay=cells[4].getContents().trim();
					price = cells[5].getContents().trim();
					occupRate = cells[6].getContents().trim();
					income = cells[7].getContents().trim();
					
					//若该行所有列值都为空，则跳过该行
					if(StringUtils.isBlank(companyCode)
							 && StringUtils.isBlank(lineCode) && StringUtils.isBlank(flightInfo) 
							 && StringUtils.isBlank(flightDay) && StringUtils.isBlank(price) 
							 && StringUtils.isBlank(occupRate) && StringUtils.isBlank(income))
						continue;
					//验证数据有效性
					sbuilder.append(this.validSheet(i,companyCode, lineCode, flightInfo,flightDay,price, occupRate, income));
					//若是有验证行数据不通过，会返回错误信息提示，则不需要组装对象了
					if(sbuilder.length() <= 0){
						vo = new plfIncomeVO();
						vo.setCompanyCode(companyCode);
						vo.setLineCode(lineCode);
						vo.setFlightInfo(flightInfo);
						
						DateCell c=(DateCell)cells[4];
						Date dd = c.getDate();						
						flightDay=df.format(dd);
						vo.setFlightDay(StringUtils.isNotEmpty(flightDay)?DateUtils.strToDate(flightDay, "yyyy-MM-dd"):new Date());						
						vo.setPrice(Long.valueOf(price));
						vo.setOccupRate(new java.math.BigDecimal(StringUtils.trim(occupRate)));
//						vo.setOccupRate(BigDecimal.valueOf(occupRate));
						vo.setIncome(new java.math.BigDecimal(StringUtils.trim(income)));
						incomes.add(vo);
						
					}
				}
				//是验证了所有行数据后，再抛出异常提示
				if(sbuilder.length() > 0)
					throw new ExcelImportException(sbuilder.toString());
			}finally{
				cells = null;
//				 id = null;
				 companyCode =null;		
				 lineCode =null;
				 flightInfo =null;
				 flightDay =null;
				 price =null;
				 occupRate =null;
				 income =null;
				 vo = null;
				sheet = null;
			}
		
		return incomes;
	}
	
	
	private String validSheet(int row, String companyCode, String lineCode,String flightInfo,
			String flightDay,String price,String occupRate,String income) throws ExcelImportException{
		row += 1;
		StringBuilder sbuilder = new StringBuilder();
//		if(StringUtils.isEmpty(id.trim())) 
//			sbuilder.append(formatNotNull(row, "序号"));
//		if(!NumberUtils.isDigits(id.trim()))
//			sbuilder.append(formatNotDigits(row, "序号"));
		if(StringUtils.isEmpty(companyCode.trim())) 
			sbuilder.append(formatNotNull(row, "航空公司"));
		if(StringUtils.isEmpty(lineCode.trim())) 
			sbuilder.append(formatNotNull(row, "航线"));
		if(StringUtils.isEmpty(flightInfo.trim())) 
			sbuilder.append(formatNotNull(row, "航班"));
//		Date  sedate = null;
		if(StringUtils.isEmpty(flightDay.trim())){
				sbuilder.append(formatNotNull(row, "航班时间"));
		} 		
		if(StringUtils.isEmpty(price.trim())) 
			sbuilder.append(formatNotNull(row, "航班价格"));
		if(StringUtils.isEmpty(occupRate.trim()))
			sbuilder.append(formatNotNull(row, "客座率"));
		if(StringUtils.isEmpty(income.trim()))
			sbuilder.append(formatNotNull(row, "收益"));
//		if(!NumberUtils.isDigits(occupRate.trim()))
//			sbuilder.append(formatNotNull(row, "客座率"));
//		if(!NumberUtils.isDigits(income.trim()))
//			sbuilder.append(formatNotNull(row, "收益"));
//		
		
		return sbuilder.toString();
	}
	
	
	private String formatNotNull(int row, String colName){
		return String.format("第[%s]行,列[%s]不可为空\n",row, colName);
	}
	
	private String formatNotDigits(int row, String colName){
		return String.format("第[%s]行,列[%s]非整型数字\n",row, colName);
	}
	private String formatRegex(int row, String colName,String format){
		if(format != null)
			return String.format("第[%s]行,列[%s]格式不正确,应为%s\n",row, colName,format);
		return  String.format("第[%s]行,列[%s]格式不正确",row, colName);
	}
	
	
	@Override
	protected boolean validateData() throws ExcelImportException {
		sheet = wb.getSheet(0);
		int cols = sheet.getColumns();
		if(cols < 7){
			throw new ExcelImportException("Excel表格不符合当前需要的格式!");
		}
		int rows = sheet.getRows();
		
		if(rows < 4){
			throw new ExcelImportException("Excel文件中没有需要导入的数据!");
		}
		
		return true;
	}

}
